package com.biscuit.page.basicservice.design.verify_code.publish.strategy;

import com.biscuit.page.basicservice.design.verify_code.publish.VerifyCodePublishAbstractDTO;
import com.biscuit.page.basicservice.design.verify_code.publish.VerifyCodePublishContext;
import com.biscuit.page.basicservice.design.verify_code.publish.VerifyCodePublishType;
import com.biscuit.page.basicservice.design.verify_code.publish.VerifyCodePublishTypeEnum;
import com.biscuit.page.basicservice.model.enums.EmailTemplateEnum;
import com.biscuit.page.basicservice.utils.BaseThreadUtil;
import com.biscuit.page.basicservice.utils.EmailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author biscuit
 * @createTime 2024/6/25 11:41
 */
@Slf4j
@Component
@VerifyCodePublishType(type = VerifyCodePublishTypeEnum.EMAIL)
public class EmailVerifyCodePublishHandlerStrategy extends AbstractVerifyCodePublishStrategy {

    @Override
    public void publish(VerifyCodePublishContext context) {
        // 调用装饰者超类，执行前置通用逻辑
        VerifyCodePublishAbstractDTO dto = super.publishBefore(context);

        // 异步向指定邮箱发送验证码邮件
        BaseThreadUtil.async(() -> EmailUtil.sendEmail(
                EmailTemplateEnum.VERIFY_CODE_TEMPLATE.getTemplate().replace("$code$", dto.getVerifyCode()),
                context.getAccount()
        ));
        log.info("EmailVerifyCodePublishHandlerStrategy publish，向邮箱 {} 发送验证码 {}", context.getAccount(), dto.getVerifyCode());

        // 调用装饰者超类，执行后置通用逻辑
        super.publishAfter(dto);
    }

}
